<?php

/**
 * static method for logging.
 * parameter dbName will be parse as database name to store log.
 * parameter tblName will be parse as table name.
 *
 * @category    SGN
 * @package
 * @subpackage
 * @copyright   Copyright (c) 2011 VNG (http://www.vng.com.vn)
 * @license     http://www.vng.com.vn
 * @version     $Id: Log.php 1413 2012-02-29 07:10:26Z locth $
 * @author      $Author: locth $
 *
 */
class SGN_Log
{

	private static $_logs = array();
	private static $_logTypes = array('file' => 'file', 'mq' => 'mq');

	/**
	 * Write log with type and data
	 * @param string $dbName
	 * @param string $tblName
	 * @param array $data
	 * @param boolean $serialize
	 * @param boolean $debug
	 * @return boolean
	 */
	public static function writeLog($dbName = '', $tblName = '', $data = array(),
								 $serialize = true, $debug = false, $type = 'log')
	{
		/**
		 * - Collect all logs info
		 * - Write them to file or queue rely on config
		 */
		//return false;
		$logConf = SGN_Application::getConfig('sgn', 'application', 'log')->toArray();
		if (!$logConf['enable']) {
			return true;
		}
		if (isset($dbName[0]) && isset($tblName[0]) && !empty($data)) {
			self::$_logs[$dbName][$tblName][] = $data;
		}

		if (empty(self::$_logs)) {
			return true;
		}

		if (!isset($dbName[0]) && !isset($tblName[0])) {
			$logData = array();
			$logData = self::$_logs;
		} else {
			$logData = array();
			if (!isset($dbName[0])) {
				$dbName = $logConf['defaulDbName'];
			}
			//Khong ghi log trong truong hop ko co tbleName
			if (!isset($tblName[0])) {
				return true;
			}
			$logData[$dbName][$tblName] = self::$_logs[$dbName][$tblName];
		}
		$logType = $logConf['type'];
		switch ($logType) {
			case self::$_logTypes['file']:
				//--------------------------------------------------------------
				//Convert array to string content to write file
				//--------------------------------------------------------------
				$logFile = date('Ymd', time());
				asort($logData);
				$logPath = $logConf['savePath'];
				$oldPath = '';
				foreach ($logData as $dbName => $tables) {
					asort($tables);
					$path = '';
					foreach ($tables as $tblName => $logs) {
						$logContent = '';
						$path = $dbName . '/' . $tblName;
						foreach ((array)$logs as $key => $message) {
							if (is_array($message)) {
								$message = serialize($message);
							}
							$logContent .= $message . "\n";
						}
						if ($oldPath != $path) {
							$folderPath = $logPath . '/' . $dbName;
							if (!is_dir($folderPath)) {
								//pr($folderPath);
								mkdir($folderPath);
								chmod($folderPath, 0777);
							}

							$filePath = $logPath . '/' . $path . $logFile . '.' . $type;

							error_log($logContent, 3, $filePath);
							$oldPath = $path;
						}
					}
					unset(self::$_logs[$dbName][$tblName]);
				}
				//vPortal_Utility::writeFile($logContent, $logFile, 'a');
				break;
			case self::$_logTypes['mq']:
				//--------------------------------------------------------------
				//Write whole of serialize array to mq
				//--------------------------------------------------------------
				break;
		}
	}

	/**
	 * Set content to write log befor page finish.
	 * @param string $type <- use for queue name or file name
	 * @param array $data
	 */
	public static function setLog($tblName, $data, $dbName = '', $debug = false)
	{
		if (!isset($dbName[0])) {
			$logConf = SGN_Application::getConfig('sgn', 'application', 'log')->toArray();
			$dbName = $logConf['defaulDbName'];
		}
		self::$_logs[$dbName][$tblName][] = $data;
	}

	/**
	 * Get log data with dbName & tblName
	 * @param string $tblName
	 * @param string $dbName
	 * @return mixed
	 */
	public static function getLog($tblName = '', $dbName = '')
	{
		if (!isset($dbName[0]) && !isset($tblName[0])) {
			return self::$_logs;
		}

		if (!isset($dbName[0])) {
			$logConf = SGN_Application::getConfig('sgn', 'application', 'log')->toArray();
			$dbName = $logConf['defaulDbName'];
		}

		if (isset($tblName[0])) {
			return isset(self::$_logs[$dbName][$tblName]) ? self::$_logs[$dbName][$tblName] : array();
		}

		return isset(self::$_logs[$dbName]) ? self::$_logs[$dbName] : array();
	}
	/**
	 * 
	 * Writing logs using error_log() function
	 * 
	 * @param string $message
	 * @param integer $message_type
	 * @param string $destination
	 * 
	 * @return void
	 * 
	 */
    public static function writeSimpleLog($message, $message_type, $destination) 
    {
        error_log($message, $message_type, $destination);
    }
}